package rhinestone.Control;

import rhinestone.Model.Order;
import rhinestone.Model.OrderContainer;
import rhinestone.Model.Product;
import rhinestone.Model.ProductContainer;

/**
 * Controls everything regarding orders.
 *
 * @author Group 2
 * @version 2010.12.16.
 */
public class OrderController {
	private Order order;
	private OrderContainer orderContainer;
	private ProductContainer productContainer;

	/**
	 * Constructor, which does nothing.
	 */
	public OrderController(OrderContainer orderContainer, ProductContainer productContainer) {
		this.orderContainer = orderContainer;
		this.productContainer = productContainer;
	}

	public void initializeOrder() {
		order = new Order();
	}

	/**
	 * Called by UI when the employee wants to add a product to the order.
     * @param barcode The bar code of the product which will be added to the
     * order
     */
	public void addProduct(String barcode, int amount) {
		Product product = productContainer.getProduct(barcode);
		order.addProduct(product, amount);
	}

	/**
	 * Called by UI when the employee wants to remove a product from the
         * order.
         */
	public void removeProduct(String barcode, int amount) {
		Product product = productContainer.getProduct(barcode);
		order.removeProduct(product, amount);
	}

	/**
	 * Called by UI when the employee is finished with adding products to be
         * ordered.
	 */
	public void confirmOrder() {
		if(order.getProductCount() > 0)
			orderContainer.addOrder(order);
		else
			System.err.println("Cannot execute order. It is empty.");
	}


        /**
	 * Called by UI when the employee wants to check the order
         * before sending it.
         */
	public Order getOrder(int id) {
		return orderContainer.getOrder(id);
	}

	public Order getCurrentOrder() {
		return order;
	}

	/**
	 * Called by UI when the employee chooses to 'cancel order'.
	 * It will destroy the current session and it's contents.
	 */
	public void cancelOrder() {
		order = null;
		System.out.println("Order canceled");
	}
}
